package org.example.config2;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.example.config3.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

//import com.bocsh.mer.service.MerchantService;
/**
 * @Author GongXl
 * @Description 认证成功就返回一个UsernamePasswordAuthenticationToken对象
 * @Date 2021/3/23 10:20
 * @Param
 * @return
 **/
@Component
class MyAuthenticationProvider implements AuthenticationProvider {

    protected Log log = LogFactory.getLog(this.getClass());

//    @Autowired
//    MerchantService ms;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Override
    public Authentication authenticate(Authentication authentication)
            throws AuthenticationException {
        log.info("now start custom authenticate process!");
        MyWebAuthenticationDetails details = (MyWebAuthenticationDetails) authentication.getDetails();

        //校验码判断
//        if(null != details.getValidcode() && !details.getValidcode().equals(details.getSessionCodeValue())) {
//            log.info("validate code error！");
//            throw new BadCredentialsException("authenticate fail！validate code error！");
//        }

        //校验码有效期
//        if((new Date()).getTime() - details.getSessionCodeTime() > 60000) {
//            log.info("validate code expired!");
//            throw new BadCredentialsException("authenticate fail！validate code expired!");
//        }

        //验密
        String inpass="";
        try {
//            inpass = ms.getPass(details.getUsername());
            inpass = "123456";
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if(!inpass.equals(details.getPassword())) {
            log.info("password error");
            throw new BadCredentialsException("authenticate fail！password not crect");
        }
        String username = details.getUsername();
        String jwtToken = jwtTokenUtil.generateTokenByUsername(username);
        details.setJwtToken(jwtToken);
        Collection<GrantedAuthority> auths=new ArrayList<GrantedAuthority>();
        auths.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(details.getUsername(), details.getPassword(), auths);
//        放到security中
        SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
        return  usernamePasswordAuthenticationToken;

    }

    @Override
    public boolean supports(Class<?> authentication) {
        return authentication.equals(UsernamePasswordAuthenticationToken.class);
    }

}